package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Iterator;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodService vodService;


	@TsLogin
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file) {
		//调用服务层方法，包含fileId和url数据
		Map<String,Object> map =  vodService.uploadTrack(file);
		 return Result.ok(map);
	}

	@TsLogin
	@Operation(summary = "保存声音信息")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo, HttpServletRequest req) {

		//获取用户id
		Long userId = null == AuthContextHolder.getUserId()?1L:AuthContextHolder.getUserId();
		//调用服务层方法
		 trackInfoService.saveTrackInfo(trackInfoVo,userId);
		return Result.ok();
	}
	@TsLogin
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable Long pageNo,
									@PathVariable Long pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery){
		Long id = null == AuthContextHolder.getUserId()?1L:AuthContextHolder.getUserId();

		trackInfoQuery.setUserId(id);
		Page<TrackInfoVo> trackInfoVoPage = new Page<>(pageNo,pageSize);
		IPage<TrackListVo> trackListVoIPage = trackInfoService.findUserTrackPage(trackInfoVoPage,trackInfoQuery);
		return Result.ok(trackListVoIPage);
	}


	@TsLogin
	@Operation(summary = "删除声音信息")
	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		Boolean result = trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	@TsLogin
	@Operation(summary = "获取声音信息")
	@GetMapping("/getTrackInfo/{id}")
	public Result getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo= trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}
	@TsLogin
	@Operation(summary = "保存修改信息")
	@PutMapping("/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable(value = "id") Long trackId, @RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}

}

